﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Utility;
using LotteryVote.Entity;
using LotteryVote.Logic;
using LotteryVote.Logic.Web;

namespace LotteryVote
{
    public partial class EditCompany : CompanyPage
    {
        public int CompanyId { get; private set; }
        CompanyFacade companyFacade = new CompanyFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            Execute(() =>
            {
                InitParams();
            });
        }

        void InitParams()
        {
            int companyId;
            if (!string.IsNullOrEmpty(Request["Company"]) && int.TryParse(Request["Company"], out companyId))
                this.CompanyId = companyId;
            if (!IsPostBack)
            {
                BindCompanyType();
                BindRegion();
                if (this.CompanyId > 0)
                    InitCompanyData();
            }
        }
        void InitCompanyData()
        {
            var company = companyFacade.GetCompany(CompanyId);
            if (company == null)
            {
                CompanyId = 0;
                return;
            }
            txt_companyName.Text = company.Name;
            txt_abbreviation.Text = company.Abbreviation;
            txt_openTime.Text = company.OpenTime.ToString();
            txt_closeTime.Text = company.CloseTime.ToString();
            ddl_companyType.SelectedValue = company.CompanyType.Id.ToString();
            ddl_region.SelectedValue = ((int)company.Region).ToString();
        }

        void BindCompanyType()
        {
            ddl_companyType.DataSource = EnumHelper.GetDescription<CompanyType>();
            ddl_companyType.DataTextField = "Description";
            ddl_companyType.DataValueField = "Value";
            ddl_companyType.DataBind();
        }

        void BindRegion()
        {
            foreach (var region in EnumHelper.ToList<Region>())
            {
                ddl_region.Items.Add(new ListItem(GetGlobalResourceObject("Resource", region.ToString()).ToString(), ((int)region).ToString()));
            }
        }


        protected void btn_submit_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                if (CompanyId > 0)
                    UpdateCompany();
                else
                    AddCompany();
            });

        }
        void AddCompany()
        {
            TimeSpan openTime = TimeSpan.Parse(txt_openTime.Text);
            TimeSpan closeTime = TimeSpan.Parse(txt_closeTime.Text);
            companyFacade.Add(txt_companyName.Text, txt_abbreviation.Text, int.Parse(ddl_companyType.SelectedValue),
                 EnumHelper.GetEnum<Region>(ddl_region.SelectedValue), openTime, closeTime);
            ShowMessage("Result", GetGlobalResourceObject("Resource", "Success").ToString());
        }
        void UpdateCompany()
        {
            TimeSpan openTime = TimeSpan.Parse(txt_openTime.Text);
            TimeSpan closeTime = TimeSpan.Parse(txt_closeTime.Text);
            companyFacade.Update(CompanyId, txt_companyName.Text, txt_abbreviation.Text, int.Parse(ddl_companyType.SelectedValue),
                EnumHelper.GetEnum<Region>(ddl_region.SelectedValue), openTime, closeTime);
            ShowMessage("Result", GetGlobalResourceObject("Resource", "Success").ToString());
        }


        #region Public
        public string GetTitle()
        {
            return string.Format("{0}{1}", (CompanyId > 0 ? GetGlobalResourceObject("Resource", "Edit") : GetGlobalResourceObject("Resource", "New")).ToString(),
                GetGlobalResourceObject("Resource", "Company"));
        }
        #endregion
    }
}